package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.RebateCategoryDAO;
import com.junzhiit.nengji.model.RebateCategory;

@Repository
public class RebateCategoryDAOImpl extends AbstractBaseDAO<RebateCategory> implements RebateCategoryDAO {
	
	@Override
	public List<RebateCategory> findAll(RebateCategory rebateCategory,int pageNumber, int maxPageRow){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from rebate_category");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(rebateCategory.getName())){
			sql.append(" and name=?");
			param.setString(rebateCategory.getName());
		}
		
		sql.append(" order by start_price asc");
		
		return super.queryForPagingList(sql.toString(), RebateCategory.class, param, pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(RebateCategory rebateCategory){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from rebate_category");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(rebateCategory.getName())){
			sql.append(" and name=?");
			param.setString(rebateCategory.getName());
		}
		
		return super.queryForInt(sql.toString(),param);
	}
	
	
	@Override
	public RebateCategory findById(String id){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from rebate_category");
		sql.append(" where is_delete=?");
		sql.append(" and id=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(id);
		
		return super.query(sql.toString(),RebateCategory.class, param);
	}
	
	@Override
	public RebateCategory findPrevById(String id){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from rebate_category ");
		sql.append(" where start_price<(select start_price from rebate_category where id=?)");
		sql.append(" and is_delete=?");
		sql.append(" order by start_price asc LIMIT 0,1");
		
		StatementParameter param=new StatementParameter();
		param.setString(id);
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.query(sql.toString(),RebateCategory.class,param);
	}
	
	@Override
	public RebateCategory findNextById(String id){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from rebate_category ");
		sql.append(" where start_price>(select start_price from rebate_category where id=?)");
		sql.append(" and is_delete=?");
		sql.append(" order by start_price asc LIMIT 0,1");
		
		StatementParameter param=new StatementParameter();
		param.setString(id);
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.query(sql.toString(),RebateCategory.class,param);
	}
	
	@Override
	public double queryMaxPrice(){
		StringBuffer sql=new StringBuffer();
		sql.append(" select ifnull(max(end_price),0.00) from rebate_category");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForDouble(sql.toString(), param);
	}

	@Override
	public RebateCategory getRebatePriceScope(Double price) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from rebate_category where ?>=start_price and ?<end_price and is_delete=?");
		StatementParameter param=new StatementParameter();
		param.setDouble(price);
		param.setDouble(price);
		param.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(),RebateCategory.class,param);
	}
}
